package com.gscitysfy.cus.drain.modules.pipe.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gscitysfy.cloudfrmwk.data.mybatis.HerodotusIdentifierGenerator;
import com.gscitysfy.cus.drain.config.BusinessException;
import com.gscitysfy.cus.drain.constants.DistrictConverter;
import com.gscitysfy.cus.drain.modules.pipe.entity.DrainPipeHiddenDangerAnalyse;
import com.gscitysfy.cus.drain.modules.pipe.entity.DrainPipeHiddenDangerRectify;
import com.gscitysfy.cus.drain.modules.pipe.entity.DrainPipeHiddenDangerRequisition;
import com.gscitysfy.cus.drain.modules.pipe.mapper.DrainPipeHiddenDangerRequisitionMapper;
import com.gscitysfy.cus.drain.modules.pipe.service.IDrainPipeHiddenDangerAnalyseService;
import com.gscitysfy.cus.drain.modules.pipe.service.IDrainPipeHiddenDangerRectifyService;
import com.gscitysfy.cus.drain.modules.pipe.service.IDrainPipeHiddenDangerRequisitionService;
import com.gscitysfy.cus.drain.modules.pipe.vo.HiddenDangerRequisitionQueryVo;
import com.gscitysfy.cus.drain.modules.pipe.vo.HiddenDangerRequisitionVo;
import com.gscitysfy.cus.drain.utils.DateUtil;
import groovy.lang.Lazy;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.http.util.Asserts;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 隐患通知单 服务实现类
 * </p>
 *
 * @author Axi
 * @since 2022-11-16 12:06:33
 */
@Service
public class DrainPipeHiddenDangerRequisitionServiceImpl extends ServiceImpl<DrainPipeHiddenDangerRequisitionMapper, DrainPipeHiddenDangerRequisition> implements IDrainPipeHiddenDangerRequisitionService {

    @Resource
    @Lazy
    IDrainPipeHiddenDangerAnalyseService analyseService;

    @Resource
    DrainPipeHiddenDangerRequisitionMapper requisitionMapper;

    @Resource
    @Lazy
    IDrainPipeHiddenDangerRectifyService rectifyService;

    @Resource
    HerodotusIdentifierGenerator herodotusIdentifierGenerator;

    /**
     * 已推送
     */
    public static final String HIDDEN_DANGER_REQUISITION_STATE_PUSHED = "1";

    /**
     * 待整改
     */
    public static final String HIDDEN_DANGER_RECTIFY_STATE_WAIT_RECTIFY = "0";


    @Override
    public IPage<DrainPipeHiddenDangerRequisition> getPageList(HiddenDangerRequisitionQueryVo queryVo) {
        IPage<DrainPipeHiddenDangerRequisition> page = requisitionMapper.getPageList(new Page<>(queryVo.getCurrent(), queryVo.getSize()), queryVo);
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            page.getRecords().forEach(item -> {
                if (!Objects.isNull(item.getCmRegflag())) {
                    item.setCmRegflagDesc(DistrictConverter.getDistrictName(String.valueOf(item.getCmRegflag())));
                }
            });
        }
        return page;

    }

    @Override
    public Boolean update(HiddenDangerRequisitionVo requisitionVo) {
        Asserts.notNull(requisitionVo.getCmKid(), "cmkid");
        DrainPipeHiddenDangerRequisition requisition = this.getById(requisitionVo.getCmKid());
        if (Objects.isNull(requisition)) {
            throw new BusinessException("通知单不存在!");
        }
        DrainPipeHiddenDangerRequisition updateVo = new DrainPipeHiddenDangerRequisition();
        BeanUtils.copyProperties(requisitionVo, updateVo);
        return this.updateById(updateVo);
    }

    @Override
    public Boolean push(HiddenDangerRequisitionVo requisitionVo) {
        Asserts.notNull(requisitionVo.getCmKid(), "cmkid");
        DrainPipeHiddenDangerRequisition requisition = this.getById(requisitionVo.getCmKid());
        if (Objects.isNull(requisition)) {
            throw new BusinessException("通知单不存在!");
        }
        DrainPipeHiddenDangerRequisition pushVo = new HiddenDangerRequisitionVo();
        BeanUtils.copyProperties(requisitionVo, pushVo);
        //修改状态
        pushVo.setRequisitionStatus(HIDDEN_DANGER_REQUISITION_STATE_PUSHED);
        //推送时间
        pushVo.setPushedTime(DateUtil.getDate());
        //推送会在整改列表新增一条数据
        DrainPipeHiddenDangerRectify rectify = new DrainPipeHiddenDangerRectify();
        rectify.setCmKid(herodotusIdentifierGenerator.nextId(rectify).longValue());
        rectify.setRectifyState(HIDDEN_DANGER_RECTIFY_STATE_WAIT_RECTIFY);
        rectify.setRequisitionId(pushVo.getCmKid());
        rectifyService.save(rectify);
        return this.updateById(pushVo);
    }

    @Override
    public HiddenDangerRequisitionVo detailById(Long id) {
        Asserts.notNull(id, "cmkid");
        DrainPipeHiddenDangerRequisition requisition = this.getById(id);
        HiddenDangerRequisitionVo requisitionVo = new HiddenDangerRequisitionVo();
        BeanUtils.copyProperties(requisition, requisitionVo);
        DrainPipeHiddenDangerAnalyse analyse = analyseService.getById(requisition.getHiddenDangerId());
        if (!Objects.isNull(requisitionVo.getCmRegflag())) {
            requisitionVo.setCmRegflagDesc(DistrictConverter.getDistrictName(String.valueOf(requisitionVo.getCmRegflag())));
        }
        if (Objects.nonNull(analyse)) {
            if (!Objects.isNull(analyse.getCmRegflag())) {
                analyse.setCmRegflagDesc(DistrictConverter.getDistrictName(String.valueOf(analyse.getCmRegflag())));
            }
            requisitionVo.setDangerAnalyse(analyse);
        }
        return requisitionVo;
    }

}
